package com.google.code.flexmetavalidator.metavalidation
{
	import mx.collections.IList;
	import mx.utils.StringUtil;

	public class ValidationTests
	{

		public static function notNull(propertyValue : Object) : Boolean
		{
			return propertyValue != null;
		}	
		
		public static function notEmpty(propertyValue : Object) : Boolean
		{
			if (propertyValue == null) return false;
			if (!(propertyValue is String)) throw new Error("notEmpty Test can be run only on String data type");
			return (StringUtil.trim(propertyValue.toString()) != "");
		}
		
		
		public static function notZero(propertyValue : Object) : Boolean
		{
			if (propertyValue == null) return true;
			var numValue : Number = Number(propertyValue);
			return (numValue is Number && numValue != 0);
		}
		
		
		public static function isNumeric(propertyValue : Object) : Boolean
		{
			if (propertyValue == null) return false;
			var numValue : Number = Number(propertyValue);
			return (numValue is Number && !isNaN(numValue));
		}
		
		public static function length(propertyValue : Object, min : Object, max : Object) : Boolean
		{
			if (propertyValue != null && !(propertyValue is String))
			{
				throw new Error("length validation test could be run only on String data type");
			}
			
			var retValue : Boolean = true;
			var minValue : Number = parseInt(String(min));
			var maxValue : Number = parseInt(String(max));
			if (!isNaN(minValue))
			{
				// Check Min length
				retValue = retValue && notNull(propertyValue);
				retValue = retValue && (propertyValue.length >= minValue);					
			}	
			
			if (!isNaN(maxValue))
			{
				if (propertyValue != null)
				{
					retValue = retValue && (propertyValue.length <= maxValue);
				}
			}
			
			return retValue;
		}
		
		public static function numericTest(propertyValue : Object, min : Object, max : Object) : Boolean
		{
			if (!propertyValue is Number) throw new Error("numericTest validation test could be run only on Number data types");
			
			var numValue : Number = Number(propertyValue);
			var retValue : Boolean = true;
			var minValue : Number = parseInt(String(min));
			var maxValue : Number = parseInt(String(max));
			if (!isNaN(minValue))
			{
				// Check Min length
				retValue = retValue && (numValue >= minValue);					
			}	
			
			if (!isNaN(maxValue))
			{
				retValue = retValue && (isNaN(numValue) || numValue <= maxValue);					
			}
			
			return retValue;
		}
		
		public static function listNotEmpty(propertyValue : Object) : Boolean
		{
			if (propertyValue is IList)
			{
				var listValue : IList = IList(propertyValue);
				return (listValue is IList && listValue.length > 0);
			}
			else if (propertyValue is Array)
			{
				var arrayValue : Array = propertyValue as Array;
				return (arrayValue is Array && arrayValue.length > 0);
			}
			else if (propertyValue == null)
			{
				return false;	
			}
			
			throw new Error("listNotEmpty validation test could be run only on IList or Array data types");
		}
		
	}
}